package com.zheng.coderepo.algorithm;

import org.junit.Assert;
import org.junit.Test;

/**
 * 1到n累加.
 * Created by zcz on 2018/5/21.
 */
public class CommonSum {

    /**
     * 等差数列
     *
     * @param n
     * @return
     */
    public int sum01(int n) {
        return (1 + n) * n / 2;
    }

    public int sum02(int n) {
        int sum = 0;
        for (int i = 1; i <= n; i++) {
            sum = sum + i;
        }
        return sum;
    }

    @Test
    public void testSum01() {
        Assert.assertTrue(sum01(100) == 5050);
    }

    @Test
    public void testSum02() {
        Assert.assertTrue(sum02(100) == 5050);
    }
}
